package com.newy.web.sys;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newy.core.BaseController;
import com.newy.domain.sys.Dict;
import com.newy.domain.sys.vo.DictItems;
import com.newy.service.sys.DictService;

/**
 * 字典管理
 * @author linzongxue 2012-2-18
 *
 */
@Controller
@RequestMapping("sys/dict")
public class DictController extends  BaseController{
	@Resource DictService dictSvc;

	@RequestMapping("getAllDictTypes") @ResponseBody
	public List<Dict> getAllDictTypes(){
		return dictSvc.getAllDictTypes();
	}

	@RequestMapping("getItemsByTypeId") @ResponseBody
	public List<Dict> getItemsByTypeId(Long typeId){
		return dictSvc.getDictItems(typeId);
	}
	
	@RequestMapping("getItemsByTypeName") @ResponseBody
	public Collection<Dict> getItemsByTypeName(String typeName){
		return dictSvc.getDictItems(typeName);
	}	
	
	@RequestMapping("saveDictType") @ResponseBody
	public Dict saveDictType(Dict dict){
		dictSvc.saveDictType(dict);
		return dict;
	}
	
	@RequestMapping("deleteDictType") @ResponseBody
	public void deleteDictType(Long[] ids){
		for (Long id : ids){
			dictSvc.deleteDictType(id);
		}
	}
	
	/**
	 * 保存指定字典类型的所有字典项，包含了添加、修改和删除
	 */
	@RequestMapping("saveDictItems") @ResponseBody
	public void saveDictItems(DictItems di){
		dictSvc.saveDictItems(di.getTypeId(), di.getItems());
	}
}
